//
//  HH_TableView.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-31.
//
//

#import "JddHH_TableView.h"
#import "JddViewControllerManager.h"
@interface JddHH_TableView ()

@end

@implementation JddHH_TableView
-(void)viewDidLoad
{
    [super viewDidLoad];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.tableView.tableFooterView = v;
    [self.tableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    groupArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[_tableData count]; ++i) {
        JddFollowNumber *follownum = [[JddFollowNumber alloc]init];
        follownum.open = 1;
        [groupArray addObject:follownum];
    }
    self.selectMatches = [NSMutableDictionary dictionary];
}

-(void)sportTeamButtonHeadClicked:(NSNumber*)position{
    JddFollowNumber *groupDic = [groupArray objectAtIndex:[position intValue]];
    //设置groupDic中的展开状态
    if(groupDic.open ==0){
        groupDic.open=1;
    }else {
        groupDic.open = 0;
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[position intValue]]
                  withRowAnimation:UITableViewRowAnimationFade];
}



-(void)openCell:(UIButton *)btn :(NSIndexPath *)indexPath
{
    NSDictionary* sectionCell = [_tableData objectAtIndex:indexPath.section];
    NSArray *Match = [sectionCell objectForKey:@"Matches"];
    NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    NSString *wk = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
    NSMutableArray *selectArray = [self.selectMatches objectForKey:indexPath];    
    NSArray *arr = [NSArray arrayWithObjects:wk, indexPath, selectArray, nil];
    [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_PICK_NUM_HH_SELECT
                                                         fromController:self
                                                             withObject:MatchCell
                                                            withObject2:arr];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSString *MCount = [sectionCell objectForKey:@"MCount"];
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            return 0;
        }else {
            return [MCount intValue];
        }
        
    }else {
        return [MCount intValue];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ) {
        return 92.0;
    }
    return 75.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JddPickNumSportItemCellHeader *cell = [JddPickNumSportItemCellHeader getInstanceView];
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSNumber *MCount = [sectionCell objectForKey:@"MCount"];
    NSString *Name = [sectionCell objectForKey:@"issue"];
    cell.day.text = Name;
    cell.Date.text = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
    cell.Mnum.text = [NSString stringWithFormat:@"共有%d场比赛可投",[MCount intValue]];
    cell.statePosition = [NSNumber numberWithInt:section];
    cell.delegate = self;
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_hall_down.png"];
        }else {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_up.png"];
        }
        
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* sectionCell = [_tableData objectAtIndex:indexPath.section];
    NSArray *Match = [sectionCell objectForKey:@"Matches"];
    NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    NSString *ETime = [MatchCell objectForKey:@"ETime"];
    NSString *HTeam = [MatchCell objectForKey:@"HTeam"];
    NSString *MID = [MatchCell objectForKey:@"MID"];
    NSString *NMm = [MatchCell objectForKey:@"NMm"];
    NSString *VTeam = [MatchCell objectForKey:@"VTeam"];
    NSString *wk = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
    JddPickNumSportItemBQCCell *cell = [JddPickNumSportItemBQCCell getInstanceView];
    [cell.label_isSingle setHidden:YES];
    cell.weakNum.text = [NSString stringWithFormat:@"%@",[MID substringFromIndex:6]];
    //截止
    cell.EndTime.text = [NSString stringWithFormat:@"%@",[ETime substringWithRange:NSMakeRange(11,5)]];
    cell.endtime = ETime;
    cell.Match.text = NMm;
    cell.HTeam.text= HTeam;
    cell.VTeam.text = VTeam;
    cell.MID = MID;
    [cell.Win setTitle:@"选择赛事方案" forState:UIControlStateNormal];
    //周三001/131107002
    NSString *codeStr = [NSString stringWithFormat:@"%@%@/%@", wk, cell.weakNum.text, MID];
    if ([self.selectMatches objectForKey:indexPath]) {
        NSArray *resultArrs = [self.selectMatches objectForKey:indexPath];
        NSArray *spfArr = [resultArrs objectAtIndex:0];
        NSArray *rqspfArr = [resultArrs objectAtIndex:1];
        NSArray *zjcArr = [resultArrs objectAtIndex:2];
        NSArray *bqcArr = [resultArrs objectAtIndex:3];
        NSArray *bfArr = [resultArrs objectAtIndex:4];
        NSMutableString *resultStr = [NSMutableString string];
        if (spfArr.count > 1) {
            NSString *spfStr = [spfArr componentsJoinedByString:@""];
            [resultStr appendString:spfStr];
        }
        if (rqspfArr.count > 1) {
            NSString *rqspStr = [rqspfArr componentsJoinedByString:@""];
            [resultStr appendString:rqspStr];
        }
        if (zjcArr.count > 1) {
            NSString *zjcStr = [zjcArr componentsJoinedByString:@""];
            [resultStr appendString:zjcStr];
        }
        if (bqcArr.count > 1) {
            NSString *bqcStr = [bqcArr componentsJoinedByString:@""];
            [resultStr appendString:bqcStr];
        }
        if (bfArr.count > 1) {
            NSString *bfStr = [bfArr componentsJoinedByString:@""];
            [resultStr appendString:bfStr];
        }
        [cell.Win setTitle:resultStr forState:UIControlStateSelected];
        cell.Win.selected = YES;
    }
    cell.aSportNumber = [[JddSportNumber alloc] init];
    cell.aSportNumber.code = codeStr;
    cell.aSportNumber.Hteam = HTeam;
    cell.aSportNumber.Vteam = VTeam;
    cell.aSportNumber.endTime = ETime;
    cell.aSportNumber.teamArray= [[NSMutableArray alloc] init];
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
}

-(void)sportTeamButtonClicked:(NSIndexPath *)indexPath AndResultStr:(NSMutableString *)resultStr AndResultArr:(NSMutableArray *)resultArr{
    JddPickNumSportItemBQCCell *cell = (JddPickNumSportItemBQCCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    cell.aSportNumber.teamArray= [NSMutableArray arrayWithArray:resultArr];
    JddSportNumber *number = cell.aSportNumber;
    if (resultStr.length > 0) {
        [cell.Win setSelected:YES];
        [cell.Win setTitle:resultStr forState:UIControlStateNormal];
        [self.sportlotterylist addBallNumberBeforeRemove:number];
        //记录每个cell的按钮显示数据
        [self.selectMatches setObject:resultArr forKey:indexPath];
    }else{
        [cell.Win setSelected:NO];
        [cell.Win setTitle:@"选择赛事方案" forState:UIControlStateNormal];
        [self.sportlotterylist removeBallNumber:number];
        //删除每个cell的按钮显示数据
        [self.selectMatches removeObjectForKey:indexPath];
    }
    if ([self.sportlotterylist.sportList count]>15) {
        [self.sportlotterylist BallHasNumber:number];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"最多选择15场比赛" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
    [self.delegate performSelector:@selector(selectMatch)];
}

@end